﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using Shpm.System;
using Shpm.App.Commons.Utils;

public partial class App_Input_TimeBankForm : System.Web.UI.Page
{ 
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ViewState["ACTION_ID"] = Context.Items["ACTION_ID"];
            HappyPlanResultDao dao = new HappyPlanResultDao();
            HappyPlanResultBean bean = new HappyPlanResultBean();
            bean.ActionResultId = Convert.ToInt32(ViewState["ACTION_ID"]);
            bean = dao.LoadHappyPlanResultByResultId(bean);

            if(bean != null)
            {
                ViewState["ACTION_NAME"] = bean.ActionName;
            }
            FindTimeBand();
        }
       
    }

    private void FindTimeBand() 
    {
        TimeBankDao dao = new TimeBankDao();
        TimeBankBean bean = new TimeBankBean();
        bean.ActionId = Convert.ToInt32(ViewState["ACTION_ID"]);
        IList list = dao.FindTimeBank(bean);
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //检查时间按输入格式
        ComValidateChecker validator = new ComValidateChecker();
        IList list = new ArrayList();
        TimeBankBean bean = null;
        TextBox txtTimeIn = null;
        TextBox txtTimeOut = null;
        string yigongId = string.Empty;
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            txtTimeIn = (GridView1.Rows[i].FindControl("txtTimeIn") as TextBox);
            txtTimeOut = (GridView1.Rows[i].FindControl("txtTimeOut") as TextBox);

            validator.Add(new CheckLong(txtTimeIn.Text, new ComMessage("E0317", "存入时间")));
            validator.Add(new CheckLong(txtTimeOut.Text, new ComMessage("E0317", "支出时间")));
        }
        if (validator.DoValidator() == false)
        {
            // 输出校验失败信息
            ((Design_MasterPage)this.Master).InfoMessage = validator.ErrorMessage;
            return;
        }
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            yigongId = Convert.ToString(GridView1.DataKeys[i].Values[0]);
            txtTimeIn = (GridView1.Rows[i].FindControl("txtTimeIn") as TextBox);
            txtTimeOut = (GridView1.Rows[i].FindControl("txtTimeOut") as TextBox);

            if (!string.IsNullOrEmpty(txtTimeIn.Text.Trim()) || !string.IsNullOrEmpty(txtTimeOut.Text.Trim()))
            {
                bean = new TimeBankBean();
                bean.ActionId = Convert.ToInt32(ViewState["ACTION_ID"]);
                bean.YigongId = Convert.ToInt32(yigongId);
                if (!string.IsNullOrEmpty(txtTimeIn.Text.Trim()))
                    bean.TimeIn = Convert.ToInt32(txtTimeIn.Text.Trim());
                if (!string.IsNullOrEmpty(txtTimeOut.Text.Trim()))
                    bean.TimeOut = Convert.ToInt32(txtTimeOut.Text.Trim());
                list.Add(bean);
            }
        }


        TimeBankDao dao = new TimeBankDao();
        dao.Insert(list);

        ((Design_MasterPage)this.Master).InfoMessage =
                             HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
        //刷新列表
        FindTimeBand();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = Convert.ToString(ViewState["ACTION_NAME"]);
        }
    }

    protected void btn_Back_Click(object sender, EventArgs e)
    {
        ControllerUtils.Transfer(this.Context, "~/App/Input/VolunteerSrhForm.aspx", true);
    }
}
